package com.shirley.dbExample.controller;

import com.shirley.dbExample.entity.SubCompany;
import com.shirley.dbExample.service.SubCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/api/v1/subCompany")
public class SubCompanyController {

    @Autowired
    private SubCompanyService subCompanyService;

    @GetMapping
    @ResponseBody
    public ResponseEntity<List<SubCompany>> findAll() {
        return ResponseEntity.ok(subCompanyService.findAll().stream().collect(Collectors.toList()));
    }

    @PostMapping
    @ResponseBody
    public ResponseEntity<List<SubCompany>> saveBatch(@RequestBody List<SubCompany> SubCompanies) {
        return ResponseEntity.ok(subCompanyService.saveAll(SubCompanies));
    }

    @GetMapping("/{subCompanyId}")
    @ResponseBody
    public ResponseEntity<SubCompany> findOne(@PathVariable("subCompanyId") Integer subCompanyId) {
        return ResponseEntity.ok(subCompanyService.find(subCompanyId));
    }
}
